package com.ztgf.user.scheduled;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.user.models.dto.UserImDTO;
import com.ztgf.user.models.dto.UserLoginDTO;
import com.ztgf.user.models.service.IUserImService;
import com.ztgf.user.models.service.IUserLoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @Description ：
 * @Author ： 解风
 * @Date ： 2018/01/26 14:10
 */
@Component
@Slf4j
public class ScheduledTimes {
    @DubboReference
    private IUserLoginService iUserLoginService;
    @DubboReference
    private IUserImService iUserImService;
    /**
     * 每5分钟执行一次 批量解锁被锁账号
     */
    @Scheduled(cron="0 0/5 * * * ?")
    public void executeBatchUnlockUserLogin() throws Exception{
        try {
            boolean flag = true;
            Long minNo = 0L;
            Integer PageNo = 50;
            do {
                List<UserLoginDTO> dtos = iUserLoginService.queryLock(minNo, PageNo);
                if (dtos != null && dtos.size() >0){
                    if(dtos.size() < PageNo){
                        flag = false;
                    }
                    for (UserLoginDTO dto : dtos){
                        minNo = dto.getId();
                        if (DateUtils.getCurrentDateTime().compareTo(dto.getUnlockTime()) >= 0){
                            //已经过期，解锁
                            unLock(dto);
                        }
                    }
                }else{
                    flag = false;
                }
            }while (flag);
            log.info("--------------定时任务：批量解锁被锁账号 ------------------");
        }catch (Exception e){
            log.error("定时任务：批量解锁被锁账号", e);
        }
    }

    /**
     * 每天1点执行一次 批量解锁被锁账号
     */
    @Scheduled(cron="0 0 1 * * ?")
    public void executeUnlockUserLogin() throws Exception{
        try {
            boolean flag = true;
            Long minNo = 0L;
            Integer PageNo = 50;
            do {
                List<UserLoginDTO> dtos = iUserLoginService.queryLock(minNo, PageNo);
                if (dtos != null && dtos.size() >0){
                    if(dtos.size() < PageNo){
                        flag = false;
                    }
                    for (UserLoginDTO dto : dtos){
                        minNo = dto.getId();
                        unLock(dto);
                    }
                }else{
                    flag = false;
                }
            }while (flag);
            log.info("--------------定时任务：凌晨批量解锁被锁账号 ------------------");
        }catch (Exception e){
            log.error("定时任务：凌晨批量解锁被锁账号", e);
        }
    }

    /**
     * 每天1点执行一次 批量清空IM咨询咨询次数
     */
    @Scheduled(cron="0 0 1 * * ?")
    public void executeEmptyUserIm() throws Exception{
        try {
            Wrapper<UserImDTO> wrapper = new EntityWrapper<>();
            wrapper.gt("consultation_num", 0);

            UserImDTO userImDTO = new UserImDTO();
            userImDTO.setConsultationNum(0);
            iUserImService.update(userImDTO, wrapper);
            log.info("--------------定时任务：凌晨批量清空IM咨询咨询次数 ------------------");
        }catch (Exception e){
            log.error("定时任务：凌晨批量清空IM咨询咨询次数", e);
        }
    }

    private void unLock(UserLoginDTO dto){
        UserLoginDTO userLoginDTO = new UserLoginDTO();
        userLoginDTO.setId(dto.getId());
        userLoginDTO.setVerifyNum(5);
        userLoginDTO.setStatus(0);
        iUserLoginService.updateById(userLoginDTO);
    }
}
